package com.spice.service.entity.form;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.HashMap;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("按页查找数据提交格式")
public class PageForm<T> {

    @ApiModelProperty(value = "查找的目标页数（必选）", example = "1")
    private Long currentPage;

    @ApiModelProperty(value = "每页查找数量（必选）", example = "10")
    private Long size;

    @ApiModelProperty(value = "排列方式(可选，example测试时需要去除\\)", example = "{\"id\":\"asc\"}")
    private HashMap<String, String> orders;

    @JsonIgnore
    public QueryWrapper<T> getQueryWrapper() {
        QueryWrapper<T> tQueryWrapper = new QueryWrapper<>();
        if (orders != null){
            for (String s : orders.keySet()) {
                if ("asc".equals(orders.get(s))) {
                    tQueryWrapper.orderByAsc(s);
                } else if ("desc".equals(orders.get(s))) {
                    tQueryWrapper.orderByDesc(s);
                }
            }
            return tQueryWrapper;
        } else  {
            return null;
        }
    }
}
